<?php
namespace app\admin\controller;
use app\admin\validate\LetterValidate;
use app\service\LetterService;
use app\service\MemberService;
use think\exception\ValidateException;

class Letter extends Admin
{
    public function __construct(LetterService $service)
    {
        parent::__construct();
        $this->service = $service;
    }

    public function index()
    {
        if (!$this->request->isPost()) {
            $this->assign("title", "通知记录");
            return $this->fetch("index");
        }
        $params = $this->request->post();
        $where = [];
        array_push($where,["is_del","=",0]);
        if (!empty($params["title"])) {
            $where[] = ['title', 'like', "%{$params['title']}%"];
        }
        $ret = $this->service->getList($where);
        $this->result($ret);
    }

    private function save()
    {
        $params = $this->request->Post();
        $data =[
            "title"=>$params["title"],
            "content"=>$params["content"],
            "thumb"=>$params["image"],
            "id"=>$params["id"] ?? 0,
            "member_id"=>$params["member_id"]
        ];
        if (!empty($data["member_id"])) {
            $memberService = $this->app->make(MemberService::class);
            $res = $memberService->get($data["member_id"],["id"]);
            if (empty($res)) {
                throw new ValidateException("会员不存在");
            }
        }
        $action = $this->request->action();
        try {
            validate(LetterValidate::class)->scene($action)->check($data);
        } catch (ValidateException $e) {
            $this->error($e->getError());
        }
        $ret = $this->service->save($data);
        if ($ret) {
            $this->success("操作成功");
        } else {
            $this->error("保存失败");
        }
    }
    public function add()
    {
        if (!$this->request->isPost()) {
            $this->assign("title","添加站内信");
            $memberId = $this->request->get("id",0);
            $this->assign("vo",["member_id"=>$memberId]);
            return $this->fetch("form");
        }
        $this->save();
    }

    public function edit()
    {
        if (!$this->request->isPost()) {
            $id = $this->request->get("id");
            if (!$id) {
                throw new ValidateException("缺少id");
            }
            $vo = $this->service->get($id);
            if (empty($vo)) {
                throw new ValidateException("站内信不存在");
            }
            $this->assign("vo",$vo);
            $this->assign("title","编辑站内信");
            return $this->fetch("form");
        }
        $this->save();
    }


    public function del()
    {
        $params = $this->request->Post();
        try {
            validate(LetterValidate::class)->scene("del")->check($params);
        } catch (ValidateException $e) {
            $this->error($e->getError());
        }
        $vo = $this->service->get($params["id"],["id"]);
        if (empty($vo)) {
            throw new ValidateException("站内信不存在");
        }
        $this->service->delete($params["id"]);
        $this->success("操作成功");
    }
}